
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: FieldReader.java 301 2010-01-18 22:56:09Z archie.cobbs $
 */

package org.dellroad.sidekar.annotation;

import java.lang.reflect.Method;
import java.util.Set;

import org.dellroad.sidekar.schema.model.SimpleFieldModel;

/**
 * Introspects {@link SidekarField} annotations.
 */
class FieldReader extends AbstractReader {

    /**
     * Look for {@link SidekarField} annotations and process them.
     */
    @Override
    public void process(Class<?> entityClass, Set<Method> methods) {
        for (Method method : methods) {
            SidekarField field = method.getAnnotation(SidekarField.class);
            if (field != null)
                this.processField(field, method);
        }
    }

    private void processField(SidekarField a, Method method) {
        SimpleFieldModel fieldModel = createSimpleField(a, method, method.getGenericReturnType(), false);
        BeanProperty prop = recordGetterModel(a, method, a.value(), fieldModel);
        fieldModel.setReadMethod(method);
        fieldModel.setWriteMethod(prop.getWriteMethod());
        log.debug("found " + a + " annotation on method " + method);
    }
}

